import {Component} from '@angular/core';
import {IonicPage, NavController, NavParams} from 'ionic-angular';
import {FormBuilder, FormControl, FormGroup, Validators} from "@angular/forms";
import {LoginService} from "../../login/login.service";
import {AppConfig} from "../../../app/appConfig";
import {CommonService} from "../../../app/common.service";
import {FormComponentService} from "../form-component/form-component.service";

/**
 * Generated class for the AccountAddPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-account-add',
  templateUrl: 'account-add.html',
})
export class AccountAddPage {
  account:any = [
    {
      key: 'certificatetype',
      val: 0,
      type: 'ion-select',
      label: '证件类型',
      inputType: 'select',
      options: [
        {
          key: '身份证',
          val: 0
        }
      ]
    },
    {
      key: 'certification',
      val: '',
      label: '证件号码',
      type: 'ion-input',
      inputType: 'text'
    },
    {
      key: 'username',
      val: '',
      type: 'ion-input',
      label: '登录账号',
      inputType: 'text'
    },
    {
      key: 'password',
      val: '',
      type: 'ion-input',
      label: '登录密码',
      inputType: 'password'
    },
    {
      key: 'roletype',
      val: '',
      label: '角色类型',
      type: 'ion-select',
      inputType: 'select',
      options: [
        {
          key: '餐馆老板',
          val: 'BOSS'
        },
        {
          key: '管理员',
          val: 'ADMIN'
        },
        {
          key: '员工',
          val: 'STAFF'
        }
      ]
    },
    {
      key: 'name',
      val: '',
      type: 'ion-input',
      label: '真实姓名',
      inputType: 'text'
    },
    {
      key: 'phonenum',
      val: '',
      type: 'ion-input',
      inputType: 'number',
      label: '手机号码'
    }
  ];
  accountForm: FormGroup;
  editMode = false;


  constructor(public navCtrl: NavController, public navParams: NavParams, public fb: FormBuilder, private loginService: LoginService, private appConfig: AppConfig, private commonService: CommonService, public serv: FormComponentService) {
    let data = this.navParams.get('data');
    let type = this.navParams.get('type');
    if (type === 'admin') {
      this.account[4].options = [{
        key: '餐馆老板',
        val: 'BOSS'
      },
        {
          key: '管理员',
          val: 'ADMIN'
        }];
    }
    else {
      this.account[4].options = [
        {
          key: '员工',
          val: 'STAFF'
        }
      ];
    }
    if (data) {
      this.editMode = true;
      console.log(data);
      this.accountForm = this.fb.group({
        certificatetype: [0, [Validators.required]],
        certification: [data.certification, [Validators.required, Validators.maxLength(20), Validators.minLength(15)]],
        username: [data.username, [Validators.required, Validators.maxLength(15), Validators.minLength(5)]],
        password: [null, [Validators.maxLength(20)]],
        roletype: [data.roletype, [Validators.required]],
        name: [data.name, [Validators.required, Validators.minLength(2), Validators.maxLength(6)]],
        phonenum: [data.phonenum, [Validators.required, Validators.minLength(6), Validators.maxLength(13)]],
        userid: [data.userid]
      });
    }
    else {
      this.accountForm = this.fb.group({
        certificatetype: [0, [Validators.required]],
        certification: ['', [Validators.required, Validators.maxLength(20), Validators.minLength(15)]],
        username: ['', [Validators.required, Validators.maxLength(15), Validators.minLength(5)]],
        password: ['', [Validators.required, Validators.maxLength(20), Validators.minLength(6)]],
        roletype: ['BOSS', [Validators.required]],
        name: ['', [Validators.required, Validators.minLength(2), Validators.maxLength(6)]],
        phonenum: ['', [Validators.required, Validators.minLength(6), Validators.maxLength(13)]],
      });
    }
    this.serv.group = this.accountForm;
  }

  ionViewWillEnter() {

  }

  formControlGet(name: string): any {
    return this.accountForm.controls[name];
  }

  get name() {
    return this.accountForm.controls['name'];
  }

  get certificatetype() {
    return this.accountForm.controls['certificatetype'];
  }

  _submitForm(val) {
    if (this.editMode)
      this.appConfig.presentLoading('正在修改...');
    else
      this.appConfig.presentLoading('正在注册...');

    this.loginService.signUp(val, this.editMode).subscribe(res => {
      if (res.code === 200) {
        if (this.editMode)
          this.appConfig.presentToast('修改成功');
        else this.appConfig.presentToast('注册成功');
        this.navCtrl.pop();
      }
      else {
        this.appConfig.presentToast(res.msg);
      }
      this.appConfig.loader.dismiss();
    }, error => {
      this.commonService.httpError(error);
      this.appConfig.loader.dismiss()
    });
  }

  output(val: any) {
    console.log(val);
    this._submitForm(val);
  }

}
